package com.lechuang.yizhezhe.view.activity.live;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.AlibcTaokeParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;
import com.lechuang.yizhezhe.R;
import com.lechuang.yizhezhe.base.Constants;
import com.lechuang.yizhezhe.model.DemoTradeCallback;
import com.lechuang.yizhezhe.presenter.net.QUrl;
import com.lechuang.yizhezhe.view.activity.SunBigPicActivity;
import com.lechuang.yizhezhe.view.defineView.ProgressWebView;


import java.util.HashMap;
import java.util.Map;


/**
 * 方法名：直播碎片
 * 方法描述：
 * 作者：韩雪松 on 2017/8/1 11:30
 * 邮箱：15245605689@163.com
 */

public class LiveFragment extends android.support.v4.app.Fragment{
    private ProgressWebView mWeb;
    private Context mContext = getActivity();
    private View v;
    private AlibcShowParams alibcShowParams;//页面打开方式，默认，H5，Native
    private Map<String, String> exParams;//yhhpass参数
    private boolean isRefulsh = true;    //是否刷新


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_live, container, false);
        //v = View.inflate(BaseApplication.getInstance(), R.layout.fragment_live, null);
        initView();
        initEvent();
        ViewGroup vg = (ViewGroup) this.v.getParent();
        if (null != vg) {
            vg.removeAllViewsInLayout();
        }
        return this.v;
    }

    //初始化view
    private void initView() {
        v.findViewById(R.id.iv_back).setVisibility(View.GONE);
        ((TextView) v.findViewById(R.id.tv_title)).setText("精彩直播");

    }

    private void initEvent() {
        mWeb = (ProgressWebView) v.findViewById(R.id.wv_live);
        //js调用
        mWeb.getSettings().setJavaScriptEnabled(true);
        //是否储存
        mWeb.getSettings().setDomStorageEnabled(true);
        //缓存大小
        //mWeb.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        //缓存路径
        //String appCachePath = getCacheDir().getAbsolutePath();
        //mWeb.getSettings().setAppCachePath(appCachePath);
        //是否禁止访问文件数据
        mWeb.getSettings().setAllowFileAccess(true);
        mWeb.getSettings().setAppCacheEnabled(true);
        mWeb.getSettings().setUseWideViewPort(true);
        mWeb.getSettings().setLoadWithOverviewMode(true);
        //是否支持缩放
        mWeb.getSettings().setSupportZoom(true);
        mWeb.getSettings().setBuiltInZoomControls(true);
        mWeb.getSettings().setDisplayZoomControls(false);
        mWeb.loadUrl(QUrl.live);
        mWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("appfun:live:")) {
                    isRefulsh = false;
                    String productUrl = url.substring(12);
                    alibcShowParams = new AlibcShowParams(OpenType.Native, false);
                    AlibcTaokeParams taoke = new AlibcTaokeParams(Constants.PID, "", "");
                    exParams = new HashMap<>();
                    exParams.put("isv_code", "appisvcode");
                    exParams.put("alibaba", "阿里巴巴");//自定义参数部分，可任意增删改
                    //Utils.E("url:"+url);
                    //view.loadUrl(url);

                    //Utils.E("cccccc:"+getIntent().getStringExtra("alipayItemId"));
                    //AlibcBasePage alibcBasePage = new AlibcDetailPage(getIntent().getStringExtra("alipayItemId"));
                    //实例化URL打开page
                    AlibcBasePage alibcBasePage = new AlibcPage(productUrl);
                    //AlibcTrade.show(ProductDetailsActivity.this, alibcBasePage, alibcShowParams, null, exParams , new DemoTradeCallback());

                    //添加购物车
                    //AlibcBasePage alibcBasePage = new AlibcAddCartPage(getIntent().getStringExtra("alipayItemId"));
                    AlibcTrade.show(getActivity(), alibcBasePage, alibcShowParams, taoke, exParams, new DemoTradeCallback());
                } else if (url.startsWith("appfun:zoomImage:")) {
                    //是否刷新
                    isRefulsh = false;
                    Intent intent = new Intent(getActivity(), SunBigPicActivity.class);
                    intent.putExtra("live", 1);
                    intent.putExtra("bigImg", url.substring(17));
                    getActivity().startActivity(intent);
                }
                return true;
            }
        });
        //WebViewUtils.loadUrl(mWeb, getActivity(), QUrl.live);
       /* mWeb.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK ) {
                        //这里处理返回键事件
                        if (mWeb.canGoBack()){
                            mWeb.goBack();
                            Toast.makeText(getActivity(), "ok", Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    }
                }
                return false;
            }
        });*/

    }

    @Override
    public void onPause() {
        super.onPause();
        //点击大图之后不刷新
        if (isRefulsh) {
            initEvent();
        }
        isRefulsh = true;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mWeb.destroy();
        //调用了AlibcTrade.show方法的Activity都需要调用AlibcTradeSDK.destory()
        AlibcTradeSDK.destory();
    }

}
